package com.DosonLeung.homeschoolcommunication.Utils;

import java.util.Calendar;

/**
 * Created by DosonLeung on 2014/10/22.
 */
public class TimeUtil {

    public static String getTime(){
        String year;
        String month;
        String date;
        String hour;
        String minute;
        String second;
        String time;
        Calendar now = Calendar.getInstance();
        year = ((Integer)(now.get(Calendar.YEAR))).toString();
        month = ((Integer)(now.get(Calendar.MONTH)+1)).toString();
        if(month.length()<2){
            month = "0"+month;
        }
        date = ((Integer)(now.get(Calendar.DAY_OF_MONTH))).toString();
        if(date.length()<2){
            date = "0"+date;
        }
        hour = ((Integer)(now.get(Calendar.HOUR_OF_DAY))).toString();
        if(hour.length()<2){
            hour = "0"+hour;
        }
        minute = ((Integer)(now.get(Calendar.MINUTE))).toString();
        if(minute.length()<2){
            minute = "0"+minute;
        }
        second = ((Integer)(now.get(Calendar.SECOND))).toString();
        if(second.length()<2){
            second = "0"+second;
        }
        time = year+month+date+hour+minute+second;
        return time;
    }

    public static String simplifyTime(String verboseTime){
        String hour = "";
        String minute = "";
        String second = "";
        String time;
        int size = verboseTime.length();
        if(verboseTime.equals("")||verboseTime.equals("无")){
            time = "";
        }else{
            try {
                hour = verboseTime.substring(size - 6, size - 4);
                minute = verboseTime.substring(size - 4, size - 2);
                second = verboseTime.substring(size - 2, size);
            } catch (StringIndexOutOfBoundsException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            time = hour + ":" + minute + ":" + second;
        }
        return time;
    }

    public static String simplfyDate(String verboseTime){
        String year = "";
        String month = "";
        String date = "";
        String time = "";
        if(verboseTime.equals("")||verboseTime.equals("无")||verboseTime.equals(" ")) {
            time = "";
        }else {
            try {
                year = verboseTime.substring(0, 4);
                month = verboseTime.substring(4, 6);
                date = verboseTime.substring(6, 8);
            } catch (StringIndexOutOfBoundsException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            time = year + "年" + month + "月" + date + "日";
        }
        return time;
    }

    public static String patchTime(String fregTime){
        String year;
        String month;
        String date;
        String hour;
        String minute;
        String second;
        String time;
        Calendar now = Calendar.getInstance();
        year = ((Integer)(now.get(Calendar.YEAR))).toString();
        month = ((Integer)(now.get(Calendar.MONTH)+1)).toString();
        if(month.length()<2){
            month = "0"+month;
        }
        date = ((Integer)(now.get(Calendar.DAY_OF_MONTH))).toString();
        if(date.length()<2){
            date = "0"+date;
        }
        try {
            hour = fregTime.substring(0, 2);
            minute = fregTime.substring(3, 5);
            second = fregTime.substring(6, 8);
            time = year + month + date + hour + minute + second;
        }catch (StringIndexOutOfBoundsException e){
            e.printStackTrace();
            time = getTime();
        }
        return time;
    }

    //标准格式的时间转为数据库格式的时间
    public static String formatTime(String simplyTime){
        String year;
        String month;
        String date;
        String hour;
        String minute;
        String second;
        String time;
        Calendar now = Calendar.getInstance();
        year = ((Integer)(now.get(Calendar.YEAR))).toString();
        month = ((Integer)(now.get(Calendar.MONTH)+1)).toString();
        if(month.length()<2){
            month = "0"+month;
        }
        date = ((Integer)(now.get(Calendar.DAY_OF_MONTH))).toString();
        if(date.length()<2){
            date = "0"+date;
        }
        try {
            hour = simplyTime.substring(11, 13);
            minute = simplyTime.substring(14, 16);
            second = simplyTime.substring(17, 19);
            time = year + month + date + hour + minute + second;
        }catch (StringIndexOutOfBoundsException e){
            e.printStackTrace();
            time = getTime();
        }
        return time;
    }

    //数据库格式的时间转为标准格式的时间
    public static String standardTime(String verboseTime){
        String year = "";
        String month = "";
        String date = "";
        String hour = "";
        String minute = "";
        String second = "";
        String time;
        int size = verboseTime.length();
        try{
            year = verboseTime.substring(0,4);
            month = verboseTime.substring(4,6);
            date = verboseTime.substring(6,8);
            hour = verboseTime.substring(size - 6, size - 4);
            minute = verboseTime.substring(size - 4, size - 2);
            second = verboseTime.substring(size - 2, size);
        }catch (StringIndexOutOfBoundsException e){
            e.printStackTrace();
        }catch (Exception e){
            e.printStackTrace();
        }
        time = year+"-"+month+"-"+date+" "+hour+":"+minute+":"+second;
        return time;
    }

    public static Avail getAvail(){
        return new Avail();
    }

    public static class Avail {
        private long lastTime = 0;
        private long interval = 1000;

        public long getInterval() {
            return interval;
        }

        public void setInterval(long interval) {
            this.interval = interval;
        }

        public boolean isAvail() {
            long time = System.currentTimeMillis();
            long timeD = time - lastTime;
            if (0 < timeD && timeD < interval) {
                return true;
            }
            lastTime = time;
            return false;
        }

    }


}
